package name.luojun.DBhelper.ORM.drivers;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.log4j.Logger;

import name.luojun.DBhelper.ORM.entity.*;
public abstract class DriverSupport {
	public abstract void createTable(Table table);
	public abstract boolean equals(Column before, Column now);
	public abstract boolean equals(PrimaryKey before, PrimaryKey now);
	public abstract Table getTable(String name);
	public abstract void addColumn(Table table, Column column);
	public abstract void modifyColumn(Table table, Column column, Column before);
	public abstract void dropColumn(Table table, Column column);
	public abstract void renameColumn(Table table, Column column, String newname);
	public abstract void dropPrimayKey(Table table);
	public abstract void addPrimayKey(Table table);
	public abstract void dropUniqueKey(Table table, UniqueKey uniqueKey);
	public abstract void addUniqueKey(Table table, UniqueKey uniqueKey);
	public abstract void dropForeignKey(Table table, ForeignKey foreignKey);
	public abstract void addForeignKey(Table table, ForeignKey foreignKey);
	public abstract boolean isCaseSensitive();
	public abstract void addcomment(Table table, Column column);
	protected Map<String, Map<String, String>> loadProperties(String path) throws IOException {
		Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
		Properties properties = new Properties();
		InputStream in = DriverSupport.class.getResourceAsStream(path);
		properties.load(in);
		for (Entry<Object, Object> entry : properties.entrySet()) {
			String k = entry.getKey().toString();
			String v = entry.getValue().toString();
			String[] keys = k.split(",");
			String[] values = v.split(",");
			Map<String, String> vmap = new HashMap<String, String>();
			for (String v_ : values) {
				String[] kv = v_.split("=");
				vmap.put(kv[0].trim().toLowerCase(), kv[1].trim());
			}
			for (String v_ : keys) {
				result.put(v_.trim(), vmap);
			}
		}
		return result;
	}
	protected Logger log = Logger.getLogger(this.getClass());
	protected Connection connection;
	public Connection releaseConnection() {
		Connection torelease = connection;
		connection = null;
		return torelease;
	}
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	public DriverSupport(Connection connection) {
		this.connection = connection;
	}
	protected void excute(String sql) {
		Statement statement = null;
		try {
			statement = connection.createStatement();
			log.debug(sql);
			statement.execute(sql);
			statement.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	protected ResultSet excuteQuery(String sql) {
		try {
			Statement statement = connection.createStatement();
			log.debug(sql);
			return statement.executeQuery(sql);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * compare length,sqltype,nullalble
	 * 
	 * @param before
	 * @param now
	 * @return
	 */
	public boolean preEquals(Column before, Column now) {
		if (now.getLength() != 0 && now.getLength() != before.getLength()) {
			return false;
		}
		if (isStrNotEmpty(now.getSqlType()) && !now.getSqlType().equalsIgnoreCase(before.getSqlType())) {
			return false;
		}
		if (now.getNullable() != before.getNullable()) {
			return false;
		}
		return true;
	}
	public boolean preEquals(PrimaryKey before, PrimaryKey now) {
		if (now.getColumns().size() != before.getColumns().size()) {
			return false;
		}
		for (String col : now.getColumns()) {
			boolean have = false;
			for (String col1 : before.getColumns()) {
				if (col.equalsIgnoreCase(col1)) {
					have = true;
				}
			}
			if (!have) {
				return false;
			}
		}
		return true;
	}
	public boolean isStrEmpty(String str) {
		if (str == null) {
			return true;
		}
		if (str.trim().length() == 0) {
			return true;
		}
		return false;
	}
	public boolean isStrNotEmpty(String str) {
		if (str == null) {
			return false;
		}
		if (str.trim().length() == 0) {
			return false;
		}
		return true;
	}
}
